package com.fionera.sample.adapter

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.fionera.sample.R
import com.fionera.sample.bean.StampListBean
import com.fionera.sample.constant.SPConstants
import com.fionera.sample.util.SPUtil
import java.util.Locale

/**
 * @author fionera
 */
class RvStampListAdapter(private val context: Context, private val list: List<StampListBean.RowsEntity>) : RecyclerView.Adapter<RvStampListAdapter.StampListViewHolder>() {
    private var onButtonClickListener: ((View, Int, Int) -> Unit)? = null

    fun setOnButtonClickListener(onButtonClickListener: ((View, Int, Int) -> Unit)) {
        this.onButtonClickListener = onButtonClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = StampListViewHolder(
            LayoutInflater.from(context).inflate(R.layout.rv_stamp_list_item, parent, false))

    override fun onBindViewHolder(holder: StampListViewHolder, position: Int) {
        holder.tvName.text = list[position].applicantUserName
        holder.tvReasonTitle.text = if (TextUtils.equals(list[position].applyType, "COUNT_LIMIT"))
            String
                    .format(Locale.CHINA, "可用次数:%s/%s次", list[position].remainTimes,
                            list[position].applyTimes)
        else
            String
                    .format(Locale.CHINA, "%s至%s", list[position].applyStartTimeReal.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0],
                            list[position].applyEndTimeReal.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0])
        holder.tvReason.text = list[position].applyReasonType.name
        holder.tvTime.text = String.format(Locale.CHINA, "申请时间:%s", list[position].applyDate)
        holder.tvTime.visibility = View.GONE
        if (TextUtils.equals(list[position].embedEquipOutFlag, "1")) {
            holder.tvStamp.setOnClickListener { v -> onButtonClickListener?.invoke(v, position, 0) }
            holder.tvStamp.text = "用印"
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.blue1))
            holder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4)
        } else {
            holder.tvStamp.setOnClickListener(null)
            holder.tvStamp.text = "审批通过"
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.text_default))
            holder.tvStamp.background = null
        }

        if (TextUtils.equals(list[position].applyCreatorTypeFlag,
                        "EQUIP_COMMON_USER") && !TextUtils.equals(SPUtil.getString(SPConstants.USER_TYPE),
                        "EQUIP_COMMON_USER")) {
            holder.tvStamp.setOnClickListener(null)
            holder.tvStamp.text = "审批通过"
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.text_default))
            holder.tvStamp.background = null
        }
    }

    override fun getItemCount() = list.size

    inner class StampListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var civHead: ImageView = itemView.findViewById(R.id.civ_stamp_list_preview)
        var tvName: TextView = itemView.findViewById(R.id.tv_stamp_list_name)
        var tvTime: TextView = itemView.findViewById(R.id.tv_stamp_list_time)
        var tvReasonTitle: TextView = itemView.findViewById(R.id.tv_stamp_list_reason_title)
        var tvReason: TextView = itemView.findViewById(R.id.tv_stamp_list_reason)
        var tvStamp: TextView = itemView.findViewById(R.id.tv_stamp_list_stamp)
    }
}
